package cn.com.util;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class StrUrl {
	String test = "app.aspx?type=getmenulist&id=1";
	// public static final String strUrl
	// ="http://61.50.149.71/api/app.aspx?type=getmenulist&id=1";
	// public static final String strUrl ="http://192.168.1.176:8098/";
	public static final String strUrl = "http://116.255.228.230:8081/";

	public static String connServerForResult(String strUrl) {
		HttpGet httpRequest = new HttpGet(strUrl);
		String strResult = "";
		try {
			HttpClient httpClient = new DefaultHttpClient();
			// 获得 httpResponse 对象
			HttpResponse response = httpClient.execute(httpRequest);
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				strResult = EntityUtils.toString(response.getEntity());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return strResult;
	}

	// 解析多个数据的Json
	public static void parseJsonMulti(String strResult) {
		try {
			JSONArray jsonObjs = new JSONObject(strResult).getJSONArray("data");
			String s = "";
			for (int i = 0; i < jsonObjs.length(); i++) {
				int id = jsonObjs.getJSONObject(i).getInt("id");
				String name = jsonObjs.getJSONObject(i).getString("name");
				String count = jsonObjs.getJSONObject(i).getString("count");
				Log.i("Other", "id=" + id + "name = " + name + "count=" + count);
			}
		} catch (JSONException e) {
			Log.i("Other", "出错了 ？");
			e.printStackTrace();
		}
	}

}
